package hyl.base.net.iot;

import hyl.core.MyFun;

/**
 * A>B1>B2>C
 * 
 * 的路径 是C+B2+B1+A
 * 
 * 目的是为了更好的索引
 * 
 * @author 37798955@qq.com
 *
 */
public class PFun {
	/**
	 * d终端名称转id
	 */
	public static int dname2Int(String name) {
		if (MyFun.isEmpty(name))
			name = MyFun.getUUID();
		return MyFun.str2int(name, name.hashCode());

	}

	/**
	 * 路径 D>C>B>A
	 * 
	 * 获取传输链路当前节点的后续节点
	 * 
	 * 
	 * @param paths
	 * @param 当前
	 * @return
	 */
	public static int getPath下层节点(int[] paths, int 当前) {
		if (paths == null || paths.length == 0)
			return -1;
		int i = paths.length - 1;
		for (; i > 0; i--) {
			if (当前 == paths[i]) {
				return paths[i - 1];
			}
		}
		return -1;

	}

	/**
	 * 求传输路径中当前节点的上级节点
	 * 
	 * @param paths
	 * @param 当前
	 * @return
	 */
	public static int getPath上层节点(int[] paths, int 当前) {
		if (paths == null || paths.length == 0)
			return -1;
		int i = 0;
		for (; i < paths.length - 1; i++) {
			if (当前 == paths[i]) {
				return paths[i + 1];
			}
		}
		return -1;
	}

	/**
	 * 获取接收方
	 * 
	 * @param paths
	 * @return
	 */
	public static int getPath接收方(int[] paths) {
		if (paths == null || paths.length == 0)
			return -1;

		return paths[0];

	}

	/**
	 * 获取发送端
	 * 
	 * @param paths
	 * @return
	 */
	public static int getPath发送方(int[] paths) {
		if (paths == null || paths.length == 0)
			return -1;

		return paths[paths.length - 1];

	}

	public static int[] addPath(int[] paths, int id) {
		if (paths == null || paths.length == 0)
			return new int[] { id };

		int[] list = new int[paths.length + 1];
		for (int i = 0; i < paths.length; i++) {
			list[i] = paths[i];
		}
		list[paths.length] = id;
		return list;
	}

}
